package com.heima.wemedia.controller.v1;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.wemedia.pojo.WmUser;
import com.heima.wemedia.service.WmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(value = "自媒体管理Api", tags = "自媒体管理Api")
@RestController
@RequestMapping("/api/v1/user")
public class WmUserController{
    @Autowired
    WmUserService wmUserService;
    @ApiOperation("保存自媒体用户")
    @PostMapping("/save")
    public WmUser save(@RequestBody WmUser wmUser) {
        // TODO == 保存自媒体用户   返回前端采用ResultResponse    WmUser  用于微服务内部
      wmUserService.save(wmUser);//只有进行了save操作,才会得到新数据的主键id,后面作者表需要用到
        return wmUser;//wmUserId
    }
    @ApiOperation("按照名称查询用户")
    @GetMapping("/findByName/{name}")
    public WmUser findByName(@PathVariable("name") String name) {
        // TODO == 查询自媒体用户
        WmUser wmUser = wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        return wmUser;//为null就需要创建新的自媒体用户,不为null,已创建好
    }

    //查询登录的用户,进而查询作者的信息,通过远程调用, 查到自媒体文章后,根据文章中关联的userId,查询自媒体用户接口,用来关联查询作者信息
    @ApiOperation("按照id查询用户")
    @GetMapping("/findOne/{id}")
    public WmUser findWmUserById(@PathVariable("id") Integer id) {
        return wmUserService.getById(id);
    }
}